package com.zhonghui.modlules.order.eshopMapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhonghui.common.dto.ChartDataDto;
import com.zhonghui.common.eshopMapper.CoreMapper;
import com.zhonghui.modlules.order.eshopDomain.StoreOrder;
import com.zhonghui.modlules.order.eshopVo.OrderDataVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public interface StoreOrderMapper extends CoreMapper<StoreOrder> {
// 1.是否删除 2.支付状态 3.退款状态  按时间进行分组GROUP BY  按时间降序 ORDER BY
@Select({"SELECT sum(pay_price) as price,count(id) as count,DATE_FORMAT(create_time, '%m-%d') as time FROM store_order WHERE is_del = 0 AND paid = 1 AND refund_status = 0 GROUP BY DATE_FORMAT(create_time,'%Y-%m-%d') ORDER BY create_time DESC"})
List<OrderDataVo> getOrderDataPriceList(Page page);
//  IFNULL 它接受两个参数，如果不是 NULL，则返回第一个参数。 否则，IFNULL 函数返回第二个参数
@Select({"SELECT IFNULL(sum(pay_price),0)  FROM store_order ${ew.customSqlSegment}"})
    Double todayPrice(@Param("ew") Wrapper<StoreOrder> wrapper);
// 根据用户的id 退款状态 是否删除 支付状态 判断 实际支付金额
@Select({"select IFNULL(sum(pay_price),0) from store_order where paid=1 and is_del=0 and refund_status=0 and uid=#{uid}"})
    double sumPrice(@Param("uid") Long uid);
//  COUNT(*) 查询 store_order这个表中的字段有多少个 查询表中支付时间大于今天的字段
@Select({"SELECT COUNT(*) FROM store_order WHERE pay_time >= ${today}"})
    Integer countByPayTimeGreaterThanEqual(@Param("today") int today);
// 查询表中支付时间小于今天的并且支付时间大于等于昨天
@Select({"SELECT COUNT(*) FROM store_order WHERE pay_time < ${today}  and pay_time >= ${yesterday}"})
    Integer countByPayTimeLessThanAndPayTimeGreaterThanEqual(@Param("today") int today, @Param("yesterday") int yesterday);
// 查询 支付状态 退款状态 是否删除  如果不是 NULL 则返回 求和的实际支付金额 否则 返回 0
@Select({"select IFNULL(sum(pay_price),0)  from store_order where refund_status=0 and is_del=0 and paid=1"})
    Double sumTotalPrice();
/**
 *   ASC 表示升序 查询 支付状态 是否删除 退款状态 支付时间 大于 创建时间
 *   如果不是 NULL 则返回 求和的实际支付金额 否则 返回 0
 *   并且 以 创建时间 进行降序 分组
 */
@Select({"SELECT IFNULL(sum(pay_price),0) as num,DATE_FORMAT(create_time, '%m-%d') as time  FROM store_order where refund_status=0 and is_del=0 and paid=1 and pay_time >= #{time} GROUP BY DATE_FORMAT(create_time,'%Y-%m-%d')  ORDER BY create_time ASC"})
    List<ChartDataDto> chartList(@Param("time") Date time);
/**
 * 根据 是否删除 退款状态 支付状态 并且 支付时间 大于 创建时间 进行查询 全部的id
 * 以创建的时间进行分组并且以创建时间进行降序
 */
@Select({"SELECT count(id) as num,DATE_FORMAT(create_time, '%m-%d') as time  FROM store_order where refund_status=0 and is_del=0 and paid=1 and pay_time >= #{time} GROUP BY DATE_FORMAT(create_time,'%Y-%m-%d')  ORDER BY create_time ASC"})
    List<ChartDataDto> chartListT(@Param("time") Date time);
}
